package com.three.lab.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.three.lab.R;
import com.three.lab.base.AppConstant;
import com.three.lab.base.BaseFragment;
import com.three.lab.bean.BannerBean;
import com.three.lab.util.AssetsUtils;
import com.three.lab.util.GsonUtils;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseFragment {

    private TextView textView,tvTitle;
    private Banner banner;
    private List<BannerBean> bannerList = new ArrayList<>();

    public static HomeFragment newInstance(int index, String title) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putInt(AppConstant.INDEX, index);
        args.putString(AppConstant.INDEX_NAME, title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initArguments(Bundle bundle) {}

    @Override
    protected int bindLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
      //  tvTitle = $(R.id.tv_title);
      //  tvTitle.setText(R.string.home);
      //  $(R.id.iv_left).setVisibility(View.GONE);
        banner = $(R.id.banner);

        textView = $(R.id.tv_home);
        String text = "How to Be a Good Student\n" +
                "We students are the builders and masters of the country. It is important for us to know how to be a good student.\n" +
                "A good student, I think, should be diligent in his studies. The more he studies, the more he will increase his knowledge. Without enough knowledge, we cannot make great contributions to the modernization of our country.\n" +
                "To take care of one's own body is another important thing for good student to do. Anyone, who hasn't got a strong body, can do nothing for his country, even if he has much knowledge. There was a man, who, when he was student, studied hard but neglected his health. No sooner did he come to serve the country than he died of poor health. From this we may see that to have a strong body is really very important for a student.\n" +
                "Lastly, to cultivate one's own virtue is most important. Virtue is the essence of a noble and good character. It will greatly help one to be useful and his country heart and soul. When learned people go astray, they do more harm than good to society. We should draw lessons from this.";
        textView.setText(text);
    }

    @Override
    public void initListener() {}

    @Override
    public void widgetClick(View v) {}

    @Override
    protected void initData(Context context) {
        initBanner();
    }

    private void initBanner() {
        String bannerRes = AssetsUtils.loadJSONFromAsset(getActivity(),"banner.json");
        bannerList = GsonUtils.parseJsonArrayWithGson(bannerRes,BannerBean.class);

       // bannerList.add(R.mipmap.banner);
       // bannerList.add(R.mipmap.banner2);
        //bannerList.add(R.mipmap.banner3);
        banner.setAdapter(new BannerImageAdapter(bannerList) {
            @Override
            public void onBindView(Object holder, Object data, int position, int size) {
                BannerBean bannerBean = bannerList.get(position);
                BannerImageHolder imageHolder = (BannerImageHolder) holder;
                imageHolder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                //图片加载自己实现
                Glide.with(getActivity())
                        .load(bannerBean.getImg())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(imageHolder.imageView);
            }
        }).setIndicator(new CircleIndicator(getActivity())).addBannerLifecycleObserver(this).setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {

            }
        }).addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}